import { getToken, setToken, removeToken } from '@/utils/auth'
import { getInfo, getAvatar } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userinfo: {}
  },
  mutations: {
    upDateToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken(state.token)
    },
    upUserInfo(state, info) {
      state.userinfo = info
    },
    clearInfo(state) {
      state.userinfo = {}
    }
  },
  actions: {
    async upDateUserInfo(context) {
      try {
        const res = await getInfo()
        const info = await getAvatar(res.data.userId)
        console.log(info)
        context.commit('upUserInfo', { ...res.data, ...info.data })
      } catch (error) {
        console.log(error)
      }
    }
  }
}

